package org.viettan.android.main;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.acra.ACRA;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;
import org.viettan.android.main.io.Logger;
import org.viettan.android.main.io.cache.CacheUtilityImpl;
import org.viettan.android.main.io.cache.ImageCacheManager;
import org.viettan.android.main.io.cache.WebCacheManager;
import org.viettan.android.main.managers.DialogManager;
import org.viettan.android.main.managers.RadioManager;
import org.viettan.android.main.managers.SettingsManager;

import roboguice.application.RoboInjectableApplication;
import android.content.Context;

import com.google.inject.Inject;
import com.google.inject.Module;

/**
 * The main Viet Tan application on the Android phone.
 */
@ReportsCrashes(formKey = "insert_your_own_google_docs_key_here",
                mode = ReportingInteractionMode.NOTIFICATION,
            	resNotifTickerText = R.string.crash_notif_ticker_text,
 		resNotifTitle = R.string.crash_notif_title,
 		resNotifText = R.string.crash_notif_text,
 		resNotifIcon = android.R.drawable.stat_notify_error, // optional. default is a warning sign
                resDialogText = R.string.crash_dialog_text,
                resDialogIcon = android.R.drawable.ic_dialog_info, //optional. default is a warning sign
                resDialogTitle = R.string.crash_dialog_title, // optional. default is your application name
                resDialogCommentPrompt = R.string.crash_dialog_comment_prompt, // optional. when defined, adds a user text field input with this text resource as a label
                resDialogOkToast = R.string.crash_dialog_ok_toast // optional. displays a Toast message when the user accepts to send a report.
                )
public class VietTanApplication extends RoboInjectableApplication {

    @Inject
    RadioManager radioManager;

    @Inject
    DialogManager notificationManager;

    @Inject
    SettingsManager settingsManager;

    @Inject
    WebCacheManager webCacheManager;

    @Inject
    ImageCacheManager imageCacheManager;

    @Inject
    Logger logger;

    private static Context context;
    private Timer periodicCleanupTask;

    @Override
    protected void addApplicationModules(List<Module> modules) {
	modules.add(new VietTanModule());
    }

    @Override
    public void onCreate() {
	ACRA.init(this);
	super.onCreate();
	context = getApplicationContext();
	settingsManager.load();

	periodicCleanupTask = new Timer("Cleanup", true);
	periodicCleanupTask.schedule(new TimerTask() {
	    @Override
	    public void run() {
		logger.info("Cleaning up cache...");
		webCacheManager.cleanup();
		imageCacheManager.cleanup();
	    }
	}, CacheUtilityImpl.CACHE_CLEANUP_PERIOD_IN_MS, CacheUtilityImpl.CACHE_CLEANUP_PERIOD_IN_MS);
    }

    @Override
    public void onTerminate() {
	super.onTerminate();
	radioManager.stop();
    }

    /**
     * Returns the application context of the application.
     */
    public static Context getAppContext() {
	return VietTanApplication.context;
    }
}
